package com.mm.gbicc.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mm.gbicc.dao.FileDao;

/**
 * Servlet implementation class DeleteMenu
 */
public class DeleteMenu extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteMenu() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter outPrintWriter = response.getWriter();
		try {
			String ids1 = request.getParameter("ids");
			String values = request.getParameter("values");
			FileDao fileDao = new FileDao();
			String ids[] = ids1.split(",");
			if(fileDao.deleteMenu(ids)){
				//System.out.println(values);
				String paths[] = values.split(",");
				String path = request.getSession().getServletContext().getRealPath("upload");
				File file = null;
				for (String fileName : paths) {
					file = new File(path+fileName);
					file.delete();
				}
				outPrintWriter.print("1");
			}else{
				outPrintWriter.print("2");
			}
		} catch (Exception e) {
			e.printStackTrace();
			outPrintWriter.print(e.toString());
		} finally{
			outPrintWriter.flush();
			outPrintWriter.close();
		}
	}
}
